package com.kly.controller;

import com.kly.common.BaseResponse;
import com.kly.common.ResultUtils;
import com.kly.domain.dto.space.SpaceAnalysisRequest;
import com.kly.domain.dto.space.SpaceRankAnalyzeRequest;
import com.kly.domain.dto.space.SpaceUserAnalysisRequest;
import com.kly.domain.entity.Space;
import com.kly.domain.entity.User;
import com.kly.domain.vo.space.*;
import com.kly.exception.ErrorCode;
import com.kly.exception.ThrowUtils;
import com.kly.service.SpaceAnalysisService;
import com.kly.service.UserService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author admin
 * @date 2025/3/17
 */
@RestController
@RequestMapping("/space/analysis")
public class SpaceAnalysisController {

    @Resource
    private SpaceAnalysisService spaceAnalysisService;

    @Resource
    private UserService userService;


    /**
     * 分析空间使用情况
     */
    @PostMapping("/usage")
    public BaseResponse<SpaceUsageAnalyzeResponse> getSpaceUsageAnalyze(@RequestBody SpaceAnalysisRequest spaceAnalysisRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(spaceAnalysisRequest == null, ErrorCode.PARAMS_ERROR, "参数为空");
        //     获取登录用户
        User loginUser = userService.getLoginUser(request);
        return ResultUtils.success(spaceAnalysisService.getSpaceUsageAnalyze(spaceAnalysisRequest, loginUser));
    }

    /**
     * 分析空间分类情况
     */
    @PostMapping("/category")
    public BaseResponse<List<SpaceCategoryAnalyzeResponse>> listSpaceCategoryAnalyze(@RequestBody SpaceAnalysisRequest spaceAnalysisRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(spaceAnalysisRequest == null, ErrorCode.PARAMS_ERROR, "参数为空");
        //     获取登录用户
        User loginUser = userService.getLoginUser(request);
        return ResultUtils.success(spaceAnalysisService.getSpaceCategoryAnalyze(spaceAnalysisRequest, loginUser));
    }

    /**
     * 分析空间标签情况
     */
    @PostMapping("/tags")
    public BaseResponse<List<SpaceTagAnalyzeResponse>> listSpaceTagAnalyze(@RequestBody SpaceAnalysisRequest spaceAnalysisRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(spaceAnalysisRequest == null, ErrorCode.PARAMS_ERROR, "参数为空");
        //     获取登录用户
        User loginUser = userService.getLoginUser(request);
        return ResultUtils.success(spaceAnalysisService.getSpaceTagAnalyze(spaceAnalysisRequest, loginUser));
    }

    /**
     * 分析空间大小情况
     */
    @PostMapping("/size")
    public BaseResponse<List<SpaceSizeAnalyzeResponse>> listSpaceSizeAnalyze(@RequestBody SpaceAnalysisRequest spaceAnalysisRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(spaceAnalysisRequest == null, ErrorCode.PARAMS_ERROR, "参数为空");
        //     获取登录用户
        User loginUser = userService.getLoginUser(request);
        return ResultUtils.success(spaceAnalysisService.getSpaceSizeAnalyze(spaceAnalysisRequest, loginUser));
    }


    /**
     * 分析空间用户情况
     */
    @PostMapping("/user")
    public BaseResponse<List<SpaceUserAnalyzeResponse>> listSpaceUserAnalyze(@RequestBody SpaceUserAnalysisRequest spaceAnalysisRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(spaceAnalysisRequest == null, ErrorCode.PARAMS_ERROR, "参数为空");
        //     获取登录用户
        User loginUser = userService.getLoginUser(request);
        return ResultUtils.success(spaceAnalysisService.getSpaceUserAnalyze(spaceAnalysisRequest, loginUser));
    }

    /**
     * 分析空间排行
     */
    @PostMapping("/rank")
    public BaseResponse<List<Space>> listSpaceRankAnalyze(@RequestBody SpaceRankAnalyzeRequest spaceRankAnalyzeRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(spaceRankAnalyzeRequest == null, ErrorCode.PARAMS_ERROR, "参数为空");
        //     获取登录用户
        User loginUser = userService.getLoginUser(request);
        return ResultUtils.success(spaceAnalysisService.getSpaceTop5Analyze(spaceRankAnalyzeRequest, loginUser));
    }


}
